package com.chuangjiangx.member.manager.client.web.stored.controller;

import com.chuangjiangx.commons.BeanUtils;
import com.chuangjiangx.commons.response.CamelResponse;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.manager.basic.web.controller.BaseController;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.basic.web.interceptor.Permissions;
import com.chuangjiangx.member.manager.client.web.stored.response.CustomRechargeResponse;
import com.chuangjiangx.member.manager.client.web.stored.response.StoredRulesListResponse;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.stored.ddd.dal.dto.RechargeRuleList;
import com.chuangjiangx.member.stored.ddd.query.MbrRechargeRuleQuery;
import com.chuangjiangx.member.stored.ddd.query.dto.CustomRechargeDTO;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.function.BiConsumer;

/**
 * 储值规则列表
 */
@Api(value = "储值规则列表",tags = "储值")
@RestController
@RequestMapping(value = "/app/member/stored-rules", produces = "application/json")
public class MbrRechargeRuleClientController extends BaseController {
    @Autowired
    private MbrRechargeRuleQuery mbrRechargeRuleQuery;




    private BiConsumer<RechargeRuleList,StoredRulesListResponse> translate = (source, target) -> {
        //赠送类型 0-积分 1-金额 2-卡券
        if (source.getGiftType() == 0) {
            target.setFreeGift("送" + source.getGiftScore().toString() + "积分");
        } else if (source.getGiftType() == 1) {
            BigDecimal giftAmount = source.getGiftAmount().setScale(2, BigDecimal.ROUND_DOWN);
            target.setFreeGift("送¥" + giftAmount.toString());
        } else if (source.getGiftType() == 2) {
            target.setFreeGift("送" + source.getGiftCoupon() + "卡券");
        } else {
            target.setFreeGift("未知");
        }
    };

    /**
     * 储值规则列表
     *
     * @param request
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @map storedRulesList List
     * @map page
     * @see StoredRulesListResponse
     * @se Page
     */
    @ApiOperation(value = "储值规则列表")
    @RequestMapping(value = "/search-list",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    @Login
    @Permissions("59014")
    public CamelResponse<List<StoredRulesListResponse>> searchStoredRulesList(HttpServletRequest request) {

        ThreadLocalUser threadLocalUser = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);

        List<RechargeRuleList> storedRulesLists = mbrRechargeRuleQuery.queryRulesList(threadLocalUser.getMerchantId());

        //对象转换
        List<StoredRulesListResponse> rulesListResponses = BeanUtils.convertCollection(storedRulesLists, StoredRulesListResponse.class, translate);
        return (CamelResponse<List<StoredRulesListResponse>>)ResponseUtils.successCamel("storedRulesList", rulesListResponses);
    }

    /**
     * 查询自定义储值开关是否开启
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @see CustomRechargeResponse
     */
    @ApiOperation(value = "查询自定义储值开关是否开启")
    @RequestMapping(value = "/check-rules-open",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    @Login
    public CamelResponse<CustomRechargeResponse> checkRulesOpen(HttpServletRequest request) {

        ThreadLocalUser threadLocalUser = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);

        CustomRechargeDTO dto = mbrRechargeRuleQuery.checkRulesOpen(threadLocalUser.getMerchantId());
        CustomRechargeResponse rulesOpenStatus = new CustomRechargeResponse();
        BeanUtils.convertBean(dto, rulesOpenStatus);

        return (CamelResponse<CustomRechargeResponse>)ResponseUtils.successCamel(rulesOpenStatus);
    }

}
